<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Repositories\GamerRepository;
use App\Repositories\GameServer\LoginRecordRepository;
use App\Repositories\GameServer\PlayRecordDetailRepository;
use App\Repositories\GameServer\ExchangeRecordRepository;
use App\Repositories\GameServer\RechargeRecordRepository;
use App\Repositories\GameServer\ChangeRecordRepository;
use App\Library\Custom\Util;

class GamerController extends namespace\BaseController
{
    protected $Repo;
    protected $LoginRecord;
    protected $PlayRecord;
    protected $ExchangeRecord;
    protected $RechargeRecord;
    protected $ChangeRecord;

    public function __construct(GamerRepository $repo, LoginRecordRepository $loginRecord, PlayRecordDetailRepository $playRecord, ExchangeRecordRepository $exchangeRecord, RechargeRecordRepository $rechargeRecord, ChangeRecordRepository $changeRecord)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
        $this->LoginRecord = $loginRecord;
        $this->PlayRecord = $playRecord;
        $this->ExchangeRecord = $exchangeRecord;
        $this->RechargeRecord = $rechargeRecord;
        $this->ChangeRecord = $changeRecord;
    }

    //玩家列表
    public function index(Request $request)
    {
        if ($request->isXmlHttpRequest()){
            $gamers = $this->Repo->getGamers($request);
            return json_encode($gamers);
        }
        $title = '玩家列表';

        return view('admin.gamers.index', compact('gamers', 'title'));
    }

    //在线玩家
    public function onlineGamers()
    {
        $gamers = $this->Repo->getOnlineGamers();
        $title = '在线玩家';
        foreach ($gamers as $val) {
            if (empty($val->userInfo)) unset($val);
        }

        return view('admin.gamers.onlineGamers', compact('gamers', 'title'));
    }

    //黑名单玩家
    public function blackListGamers()
    {
        $gamers = $this->Repo->getBlackListGamers();
        $title = '黑名单';
// dd($gamers);
        return view('admin.gamers.blacklist', compact('gamers', 'title'));
    }

    //禁言列表
    public function forbiddenGamers()
    {
        $gamers = $this->Repo->getforbiddenGamers();
        $title = '禁言';

        return view('admin.gamers.blacklist', compact('gamers', 'title'));
    }

    //玩家详情
    public function show($id)
    {
        $gamer = $this->Repo->getGamer($id);
        $login_count = $this->LoginRecord->getLoginRecordsCount($id);//登录次数
        $login_logs = $this->LoginRecord->getLoginRecords($id);//登录日志
        $play_records = $this->PlayRecord->getPlayRecords($id);//游戏详细记录
        // dd($play_records);
        $recharge_records = $this->RechargeRecord->getRechargeRecords($id);//充值记录
        $change_records   = $this->ChangeRecord->getChangeRecords($id); //账变记录
        $exchange_records = $this->ExchangeRecord->getExchangeRecords($id);//兑换记录

        $data = [];
        foreach ($recharge_records as $val) {
            $data[] = [
                'time' => $val->buy_time,
                'reason' => $val->reason,
                'total_jeton' => $val->total_jeton
            ];
        }
        foreach ($change_records as $val) {
            $data[] = [
                'time' => date('Y-m-d H:i:s', $val->occur_time),
                'reason' => $val->reason,
                'total_jeton' => $val->final_jeton
            ];
        }

        $data = Util::arrSort($data, 'SORT_DESC', 'time');

        return view('admin.gamers.show', compact('gamer', 'login_count', 'login_logs', 'play_records', 'data', 'exchange_records'));
    }

    //加入黑名单
    public function pushBlackList(Request $request, $id)
    {
        $res = $this->Repo->pushBlackList($request, $id);
        // if ($res) { //莫名奇妙搞不清为什么
        //     return $this->responseJson(0, '操作成功');
        // } else {
        //     return $this->responseJson(1, '操作失败');
        // }

        if ($res) {
            $data = [
                'status' => 0,
                'msg'    => '操作成功',
                'url'    => $_SERVER['HTTP_REFERER']
            ];
        } else {
            $data = [
                'status' => 1,
                'msg' => '操作失败',
            ];
        }


        return json_encode($data);
    }

    //移出黑名单
    public function popBlackList($id)
    {
        $res = $this->Repo->popBlackList($id);
        if ($res) {
            return $this->responseJson(0, '操作成功');
        } else {
            return $this->responseJson(1, '操作失败');
        }
    }

    //删除玩家
    public function destroy($id)
    {
        $res = $this->Repo->destroyGamer($id);
        if ($res) {
            return $this->responseJson(0, '操作成功', route('gamers.index'));
        } else {
            return $this->responseJson(1, '操作失败');
        }
    }

    //批量删除
    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);
        $res = $this->Repo->destroyList($ids);
        if ($res) {
            return $this->responseJson(0, '操作成功', route('gamers.index'));
        } else {
            return $this->responseJson(1, '操作失败');
        }
    }
}
